import {
	_decorator,
	Component,
	Node,
	EventTouch,
	UITransform,
	Collider2D,
	Contact2DType,
	Vec3,
} from 'cc';
import { MatchSpace } from './MatchSpace';
import { MultipleAudioControler } from '../logicalReasoning/MultipleAudioControler';

const { ccclass, property } = _decorator;

@ccclass('Matchstick')
export class Matchstick extends Component {
	static movingNode: Node | null = null;
	collider: Node[] = [];
	startParent!: Node;
	parent: Node | null;
	player: MultipleAudioControler;
	start() {
		this.initEvent();
		this.initCollider();
		this.parent = this.node.parent;
		this.player = this.node.getComponent(MultipleAudioControler);
	}
	initEvent() {
		this.node.on(Node.EventType.TOUCH_START, this.touchstart, this);
		this.node.on(Node.EventType.TOUCH_MOVE, this.touchmove, this);
		this.node.on(Node.EventType.TOUCH_END, this.touchend, this);
		this.node.on(Node.EventType.TOUCH_CANCEL, this.touchcancel, this);
	}
	protected onDestroy(): void {
		this.node.off(Node.EventType.TOUCH_START, this.touchstart, this);
		this.node.off(Node.EventType.TOUCH_MOVE, this.touchmove, this);
		this.node.off(Node.EventType.TOUCH_END, this.touchend, this);
		this.node.off(Node.EventType.TOUCH_CANCEL, this.touchcancel, this);
		let collider = this.node.getComponent(Collider2D);
		if (collider) {
			collider.off(Contact2DType.BEGIN_CONTACT, this.onBeginContact, this);
			collider.off(Contact2DType.END_CONTACT, this.onEndContact, this);
		}
	}

	initCollider() {
		let collider = this.node.getComponent(Collider2D);
		if (collider) {
			collider.on(Contact2DType.BEGIN_CONTACT, this.onBeginContact, this);
			collider.on(Contact2DType.END_CONTACT, this.onEndContact, this);
		}
	}
	onBeginContact(self: Collider2D, other: Collider2D) {
		this.collider.push(other.node);
	}
	onEndContact(self: Collider2D, other: Collider2D) {
		let index = this.collider.findIndex((node) => node === other.node);
		if (index >= 0) {
			this.collider.splice(index, 1);
		}
	}
	canMove() {
		return Matchstick.movingNode === this.node;
	}
	touchstart() {
		if (!Matchstick.movingNode) Matchstick.movingNode = this.node;
		this.startParent = this.node.getParent(); // 最初的父节点
		let position = this.node.getPosition();
		let ui = this.node.parent.getComponent(UITransform);
		let w_position = ui.convertToWorldSpaceAR(position);
		let p_ui = this.node.parent.parent.getComponent(UITransform);
		let p_position = p_ui.convertToNodeSpaceAR(w_position);
		this.node.setParent(this.node.parent.parent, false);
		this.node.setPosition(p_position);
		let matchSpace = this.parent.getComponent(MatchSpace);
		let rotated = matchSpace.rotated;
		this.node.angle = rotated ? -90 : 0;
	}
	touchmove(e: EventTouch) {
		if (!this.canMove) return;
		let delta = e.getUIDelta();
		let position = this.node.getPosition();
		this.node.setPosition(position.add(delta.toVec3()));
	}
	touchend() {
		Matchstick.movingNode = null;
		let length = this.collider.length;
		let collider: Node;
		if (length == 1) {
			collider = this.collider[length - 1];
		} else if (length == 2) {
			if (
				this.getDistanceOfNodes(this.node, this.collider[0]) >
				this.getDistanceOfNodes(this.node, this.collider[1])
			) {
				collider = this.collider[1];
			} else {
				collider = this.collider[0];
			}
		}
		if (collider && !collider.children.length) {
			this.node.setParent(collider);
			this.updateParentDig(this.parent);
			this.parent = collider;
			this.updateParentDig(this.parent);
		} else {
			this.node.setParent(this.parent);
		}
		if (this.startParent === this.parent) {
			this.player.play('move_f');
		} else {
			this.player.play('move_s');
		}
		this.node.angle = 0;
		this.collider = [];
	}
	getDistanceOfNodes(a: Node, b: Node) {
		return Vec3.distance(a.worldPosition, b.worldPosition);
	}
	touchcancel() {
		this.touchend();
	}
	updateParentDig(node) {
		let matchSpace = node.getComponent(MatchSpace);
		matchSpace.childChanged();
	}
}
